package com.wangzeqi.neihanduanzi.home.jokes;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.web.WebViewActivity;
import com.wangzeqi.neihanduanzi.cache.AllLruCacheLoad;
import com.wangzeqi.neihanduanzi.entity.AllEntity;
import com.wangzeqi.neihanduanzi.entity.GroupEntity;

import com.wangzeqi.neihanduanzi.singleton.QueueSingleton;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by dllo on 16/1/11.
 */
public class AdapterJokes extends RecyclerView.Adapter {

    private Context context;
    private AllEntity allEntity;

    public AdapterJokes(Context context) {
        this.context = context;
    }

    public void addData(AllEntity allEntity) {
        this.allEntity = allEntity;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout.jokes_item, null);
            JokesViewHolder viewHolder = new JokesViewHolder(view);
            return viewHolder;
        } else if (viewType == 2) {
            View view = LayoutInflater.from(context).inflate(R.layout.jokes_item_two, null);
            JokesViewHolderTwo viewHolderTwo = new JokesViewHolderTwo(view);
            return viewHolderTwo;
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.null_item, null);
            JokesViewHolderNull viewHolderNull = new JokesViewHolderNull(view);
            return viewHolderNull;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        AllLruCacheLoad cache = new AllLruCacheLoad();
        final ImageLoader imageloader = new ImageLoader(QueueSingleton.getInstance().getQueue(), cache);
        if (getItemViewType(position) == 1 && allEntity.getData().getData().get(position).getGroup() != null) {
            JokesViewHolder viewHolder = (JokesViewHolder) holder;
            final GroupEntity entity = allEntity.getData().getData().get(position).getGroup();
            viewHolder.name.setText(entity.getUser().getName());
            viewHolder.content.setText(entity.getContent());
            viewHolder.type.setText("" + entity.getCategory_name());
            viewHolder.like.setText("" + entity.getDigg_count());
            viewHolder.dislike.setText("" + entity.getBury_count());
            viewHolder.comment.setText("" + entity.getComment_count());
            viewHolder.forward.setText("" + entity.getShare_count());
            viewHolder.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            viewHolder.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            viewHolder.imageView.setImageUrl(entity.getUser().getAvatar_url(), imageloader);
            viewHolder.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showShare();
                }
            });
            viewHolder.support.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "点赞", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolder.disSupport.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "不喜欢", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolder.imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addDialog();

                }
            });
            viewHolder.personHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "个人主页", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolder.detailHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url", "http://neihanshequ.com/p" + entity.getShare_url().substring(37));
                    context.startActivity(intent);
                }
            });
        } else if (getItemViewType(position) == 2 && allEntity.getData().getData().get(position).getGroup() != null) {
            JokesViewHolderTwo viewHolderTwo = (JokesViewHolderTwo) holder;
            final GroupEntity entity = allEntity.getData().getData().get(position).getGroup();
            viewHolderTwo.name.setText(entity.getUser().getName());
            viewHolderTwo.content.setText(entity.getContent());
            viewHolderTwo.type.setText(entity.getCategory_name());
            viewHolderTwo.like.setText("" + entity.getDigg_count());
            viewHolderTwo.dislike.setText("" + entity.getBury_count());
            viewHolderTwo.comment.setText("" + entity.getComment_count());
            viewHolderTwo.forward.setText("" + entity.getShare_count());
            viewHolderTwo.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            viewHolderTwo.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            viewHolderTwo.imageView.setImageUrl(entity.getUser().getAvatar_url(), imageloader);
            viewHolderTwo.commentImageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            viewHolderTwo.commentImageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            viewHolderTwo.commentImageView.setImageUrl(allEntity.getData().getData().get(position).getComments().get(0).getAvatar_url(), imageloader);
            viewHolderTwo.commentName.setText(allEntity.getData().getData().get(position).getComments().get(0).getUser_name());
            viewHolderTwo.commentLike.setText("" + allEntity.getData().getData().get(position).getComments().get(0).getDigg_count());
            viewHolderTwo.commentContent.setText("" + allEntity.getData().getData().get(position).getComments().get(0).getText());
            viewHolderTwo.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showShare();
                }
            });
            viewHolderTwo.support.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "点赞", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolderTwo.disSupport.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "不喜欢", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolderTwo.imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addDialog();
                }
            });
            viewHolderTwo.personHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "个人主页", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolderTwo.detailHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url", "http://neihanshequ.com/p" + entity.getShare_url().substring(37));
                    context.startActivity(intent);
                }
            });
        } else {
            JokesViewHolderNull viewHolderNull = (JokesViewHolderNull) holder;
            viewHolderNull.textView.setText(null);
        }
    }

    private void showShare() {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(context.getString(R.string.ssdk_oks_share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(context.getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(context);
    }

    @Override
    public int getItemViewType(int position) {
        if (allEntity.getData().getData().get(position).getGroup() != null
                && (allEntity.getData().getData().get(position).getComments() == null ||
                allEntity.getData().getData().get(position).getComments().size() == 0)) {
            return 1;
        } else if (allEntity.getData().getData().get(position).getGroup() != null
                && allEntity.getData().getData().get(position).getComments().size() > 0) {
            return 2;
        } else {
            return 3;
        }
    }

    @Override
    public int getItemCount() {
        if (allEntity != null) {
            if (allEntity.getData() != null) {
                return allEntity.getData().getData().size();
            }
        }
        return 0;
    }

    class JokesViewHolder extends RecyclerView.ViewHolder {
        TextView name, like, dislike, comment, forward, content, type;
        NetworkImageView imageView;
        ImageButton imageButton;
        LinearLayout personHome;
        RelativeLayout detailHome, share, support, disSupport;

        public JokesViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.jokes_person_name);
            like = (TextView) itemView.findViewById(R.id.jokes_like_num);
            dislike = (TextView) itemView.findViewById(R.id.jokes_dislike_num);
            comment = (TextView) itemView.findViewById(R.id.jokes_comments_num);
            forward = (TextView) itemView.findViewById(R.id.jokes_forwarding_num);
            content = (TextView) itemView.findViewById(R.id.jokes_jokes_content);
            type = (TextView) itemView.findViewById(R.id.jokes_content_type);
            imageView = (NetworkImageView) itemView.findViewById(R.id.jokes_person_picture);
            imageButton = (ImageButton) itemView.findViewById(R.id.jokes_dislike_del);
            personHome = (LinearLayout) itemView.findViewById(R.id.jokes_person_home);
            detailHome = (RelativeLayout) itemView.findViewById(R.id.jokes_details);
            share = (RelativeLayout) itemView.findViewById(R.id.jokes_fx);
            support = (RelativeLayout) itemView.findViewById(R.id.jokes_support);
            disSupport = (RelativeLayout) itemView.findViewById(R.id.jokes_dis_support);

        }
    }

    class JokesViewHolderTwo extends RecyclerView.ViewHolder {
        TextView name, like, dislike, comment, forward, content, type, commentName, commentContent, commentLike;
        NetworkImageView imageView, commentImageView;
        ImageButton imageButton;
        LinearLayout personHome;
        RelativeLayout detailHome, share, support, disSupport;

        public JokesViewHolderTwo(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.jokes_person_name);
            like = (TextView) itemView.findViewById(R.id.jokes_like_num);
            dislike = (TextView) itemView.findViewById(R.id.jokes_dislike_num);
            comment = (TextView) itemView.findViewById(R.id.jokes_comments_num);
            forward = (TextView) itemView.findViewById(R.id.jokes_forwarding_num);
            content = (TextView) itemView.findViewById(R.id.jokes_jokes_content);
            type = (TextView) itemView.findViewById(R.id.jokes_content_type);
            imageView = (NetworkImageView) itemView.findViewById(R.id.jokes_person_picture);
            commentName = (TextView) itemView.findViewById(R.id.jokes_comments_person_name);
            commentImageView = (NetworkImageView) itemView.findViewById(R.id.jokes_comments_person_picture);
            commentContent = (TextView) itemView.findViewById(R.id.jokes_comments_person_text);
            commentLike = (TextView) itemView.findViewById(R.id.jokes_comments_like_num);
            imageButton = (ImageButton) itemView.findViewById(R.id.jokes_dislike_del);
            personHome = (LinearLayout) itemView.findViewById(R.id.jokes_person_home);
            detailHome = (RelativeLayout) itemView.findViewById(R.id.jokes_details);
            share = (RelativeLayout) itemView.findViewById(R.id.jokes_fx);
            support = (RelativeLayout) itemView.findViewById(R.id.jokes_support);
            disSupport = (RelativeLayout) itemView.findViewById(R.id.jokes_dis_support);

        }
    }

    class JokesViewHolderNull extends RecyclerView.ViewHolder {
        TextView textView;

        public JokesViewHolderNull(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.null_tv);
        }
    }

    private void addDialog() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_one, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        builder.setTitle("选择不喜欢的理由");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, "我们将减少此类消息的推送", Toast.LENGTH_SHORT).show();

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }
}
